package com.example.gaojt.androiddemo;

import java.lang.reflect.Method;

/**
 * @author guofeng.wu
 * @Description: 反射获取系统属性
 * @date 2016/7/6 15:05
 * @copyright TCL-MIG
 */

public class FeedbackSystemProperties {
    protected static final String TAG = "FeedbackSystemProperties";

    // String SystemProperties.get(String key){}
    public static String get(String key) {
        init();

        String value = null;

        try {
            value = (String) mGetMethod.invoke(mClassType, key);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return value;
    }

    // int SystemProperties.get(String key, int def){}
    public static int getInt(String key, int def) {
        init();

        int value = def;
        try {
            Integer v = (Integer) mGetIntMethod.invoke(mClassType, key, def);
            value = v.intValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    // SystemProperties.set(String key, String value){}
    public static void set(String key, String values) {
        init();
        try {
            mSetMethod.invoke(mClassType, key, values);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String getDevice() {
        return get("ro.product.device");
    }

    public static String getPTS() {
        return get("ro.tct.trace.pts");
    }

    public static String getPTH() {
        return get("ro.tct.trace.pth");
    }

    public static String getBSN() {
        return get("ro.tct.trace.bsn");
    }

    public static String getCU() {
        return get("ro.tct.curef");
    }

    private static Class<?> mClassType = null;
    private static Method mGetMethod = null;
    private static Method mGetIntMethod = null;
    private static Method mSetMethod = null;

    private static void init() {
        try {
            if (mClassType == null) {
                mClassType = Class.forName("android.os.SystemProperties");

                mGetMethod = mClassType.getDeclaredMethod("get", String.class);
                mGetIntMethod = mClassType.getDeclaredMethod("getInt", String.class, int.class);
                mSetMethod = mClassType.getDeclaredMethod("set", String.class);
            }
        } catch (Exception e) {
            /* e.printStackTrace(); */
        }
    }


}
